home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / libs / readline / complete.c < prev    next >
C/C++ Source or Header  |  1996-11-01  |  41KB  |  1,462 lines

  1. /* complete.c -- filename completion for readline. */
  2.  
  3. /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
  4.  
  5.    This file is part of the GNU Readline Library, a library for
  6.    reading lines of text with interactive input and history editing.
  7.  
  8.    The GNU Readline Library is free software; you can redistribute it
  9.    and/or modify it under the terms of the GNU General Public License
  10.    as published by the Free Software Foundation; either version 1, or
  11.    (at your option) any later version.
  12.  
  13.    The GNU Readline Library is distributed in the hope that it will be
  14.    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15.    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    The GNU General Public License is often shipped with GNU software, and
  19.    is generally kept in a file called COPYING or LICENSE.  If you do not
  20.    have a copy of the license, write to the Free Software Foundation,
  21.    675 Mass Ave, Cambridge, MA 02139, USA. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #define READLINE_LIBRARY
  28.  
  29. #include <stdio.h>
  30. #include <sys/types.h>
  31. #include <fcntl.h>
  32. #if defined (HAVE_SYS_FILE)
  33. #  include <sys/file.h>
  34. #endif /* HAVE_SYS_FILE */
  35.  
  36. #if defined (HAVE_UNISTD_H)
  37. #  include <unistd.h>
  38. #endif /* HAVE_UNISTD_H */
  39.  
  40. #if defined (HAVE_STDLIB_H)
  41. #  include <stdlib.h>
  42. #else
  43. #  include "ansi_stdlib.h"
  44. #endif /* HAVE_STDLIB_H */
  45.  
  46. #include <errno.h>
  47. /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
  48. #if !defined (errno)
  49. extern int errno;
  50. #endif /* !errno */
  51.  
  52. #include <pwd.h>
  53. #if defined (USG) && !defined (HAVE_GETPW_DECLS)
  54. extern struct passwd *getpwent ();
  55. #endif /* USG && !HAVE_GETPW_DECLS */
  56.  
  57. /* ISC systems don't define getpwent() if _POSIX_SOURCE is defined. */
  58. #if defined (isc386) && defined (_POSIX_SOURCE)
  59. #  if defined (__STDC__)
  60. extern struct passwd *getpwent (void);
  61. #  else
  62. extern struct passwd *getpwent ();
  63. #  endif /* !__STDC__ */
  64. #endif /* isc386 && _POSIX_SOURCE */
  65.  
  66. #include "posixstat.h"
  67.  
  68. /* System-specific feature definitions and include files. */
  69. #include "rldefs.h"
  70.  
  71. /* Some standard library routines. */
  72. #include "readline.h"
  73.  
  74. /* Possible values for do_replace in rl_complete_internal. */
  75. #define NO_MATCH    0
  76. #define SINGLE_MATCH    1
  77. #define MULT_MATCH    2
  78.  
  79. #if !defined (strchr) && !defined (__STDC__)
  80. extern char *strchr (), *strrchr ();
  81. #endif /* !strchr && !__STDC__ */
  82.  
  83. extern char *tilde_expand ();
  84. extern char *rl_copy_text ();
  85.  
  86. extern Function *rl_last_func;
  87. extern int rl_editing_mode;
  88. extern int screenwidth;
  89.  
  90. /* Forward declarations for functions defined and used in this file. */
  91. char *filename_completion_function ();
  92. char **completion_matches ();
  93.  
  94. static int compare_strings ();
  95. static char *rl_strpbrk ();
  96.  
  97. #if defined (STATIC_MALLOC)
  98. static char *xmalloc (), *xrealloc ();
  99. #else
  100. extern char *xmalloc (), *xrealloc ();
  101. #endif /* STATIC_MALLOC */
  102.  
  103. /* If non-zero, then this is the address of a function to call when
  104.    completing on a directory name.  The function is called with
  105.    the address of a string (the current directory name) as an arg. */
  106. Function *rl_directory_completion_hook = (Function *)NULL;
  107.  
  108. /* Non-zero means readline completion functions perform tilde expansion. */
  109. int rl_complete_with_tilde_expansion = 0;
  110.  
  111. /* If non-zero, non-unique completions always show the list of matches. */
  112. int _rl_complete_show_all = 0;
  113.  
  114. #if defined (VISIBLE_STATS)
  115. #  if !defined (X_OK)
  116. #    define X_OK 1
  117. #  endif
  118.  
  119. static int stat_char ();
  120.  
  121. /* Non-zero means add an additional character to each filename displayed
  122.    during listing completion iff rl_filename_completion_desired which helps
  123.    to indicate the type of file being listed. */
  124. int rl_visible_stats = 0;
  125. #endif /* VISIBLE_STATS */
  126.  
  127. /* **************************************************************** */
  128. /*                                    */
  129. /*    Completion matching, from readline's point of view.        */
  130. /*                                    */
  131. /* **************************************************************** */
  132.  
  133. /* Pointer to the generator function for completion_matches ().
  134.    NULL means to use filename_entry_function (), the default filename
  135.    completer. */
  136. Function *rl_completion_entry_function = (Function *)NULL;
  137.  
  138. /* Pointer to alternative function to create matches.
  139.    Function is called with TEXT, START, and END.
  140.    START and END are indices in RL_LINE_BUFFER saying what the boundaries
  141.    of TEXT are.
  142.    If this function exists and returns NULL then call the value of
  143.    rl_completion_entry_function to try to match, otherwise use the
  144.    array of strings returned. */
  145. CPPFunction *rl_attempted_completion_function = (CPPFunction *)NULL;
  146.  
  147. /* Non-zero means to suppress normal filename completion after the
  148.    user-specified completion function has been called. */
  149. int rl_attempted_completion_over = 0;
  150.  
  151. /* Local variable states what happened during the last completion attempt. */
  152. static int completion_changed_buffer = 0;
  153.  
  154. /* Complete the word at or before point.  You have supplied the function
  155.    that does the initial simple matching selection algorithm (see
  156.    completion_matches ()).  The default is to do filename completion. */
  157.  
  158. rl_complete (ignore, invoking_key)
  159.      int ignore, invoking_key;
  160. {
  161.   if (rl_last_func == rl_complete && !completion_changed_buffer)
  162.     return (rl_complete_internal ('?'));
  163.   else if (_rl_complete_show_all)
  164.     return (rl_complete_internal ('!'));
  165.   else
  166.     return (rl_complete_internal (TAB));
  167. }
  168.  
  169. /* List the possible completions.  See description of rl_complete (). */
  170. rl_possible_completions (ignore, invoking_key)
  171.      int ignore, invoking_key;
  172. {
  173.   return (rl_complete_internal ('?'));
  174. }
  175.  
  176. rl_insert_completions (ignore, invoking_key)
  177.      int ignore, invoking_key;
  178. {
  179.   return (rl_complete_internal ('*'));
  180. }
  181.  
  182. /* The user must press "y" or "n". Non-zero return means "y" pressed. */
  183. get_y_or_n ()
  184. {
  185.   int c;
  186.  
  187.   for (;;)
  188.     {
  189.       c = rl_read_key ();
  190.       if (c == 'y' || c == 'Y' || c == ' ')
  191.     return (1);
  192.       if (c == 'n' || c == 'N' || c == RUBOUT)
  193.     return (0);
  194.       if (c == ABORT_CHAR)
  195.     rl_abort ();
  196.       ding ();
  197.     }
  198. }
  199.  
  200. /* Up to this many items will be displayed in response to a
  201.    possible-completions call.  After that, we ask the user if
  202.    she is sure she wants to see them all. */
  203. int rl_completion_query_items = 100;
  204.  
  205. /* The basic list of characters that signal a break between words for the
  206.    completer routine.  The contents of this variable is what breaks words
  207.    in the shell, i.e. " \t\n\"\\'`@$><=" */
  208. char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(";
  209.  
  210. /* The list of characters that signal a break between words for
  211.    rl_complete_internal.  The default list is the contents of
  212.    rl_basic_word_break_characters.  */
  213. char *rl_completer_word_break_characters = (char *)NULL;
  214.  
  215. /* List of characters which can be used to quote a substring of the line.
  216.    Completion occurs on the entire substring, and within the substring
  217.    rl_completer_word_break_characters are treated as any other character,
  218.    unless they also appear within this list. */
  219. char *rl_completer_quote_characters = (char *)NULL;
  220.  
  221. /* Character to add after a single completion alternative matches
  222.    at the end of line.  By default this is a space.
  223.    Nothing is added if this is '\0'. */
  224. char rl_completion_append_character = ' ';
  225.  
  226. /* List of characters that are word break characters, but should be left
  227.    in TEXT when it is passed to the completion function.  The shell uses
  228.    this to help determine what kind of completing to do. */
  229. char *rl_special_prefixes = (char *)NULL;
  230.  
  231. /* If non-zero, then disallow duplicates in the matches. */
  232. int rl_ignore_completion_duplicates = 1;
  233.  
  234. /* Non-zero means that the results of the matches are to be treated
  235.    as filenames.  This is ALWAYS zero on entry, and can only be changed
  236.    within a completion entry finder function. */
  237. int rl_filename_completion_desired = 0;
  238.  
  239. /* Non-zero means that the results of the matches are to be quoted using
  240.    double quotes (or an application-specific quoting mechanism) if the
  241.    filename contains any characters in rl_word_break_chars.  This is
  242.    ALWAYS non-zero on entry, and can only be changed within a completion
  243.    entry finder function. */
  244. int rl_filename_quoting_desired = 1;
  245.  
  246. /* This function, if defined, is called by the completer when real
  247.    filename completion is done, after all the matching names have been
  248.    generated. It is passed a (char**) known as matches in the code below.
  249.    It consists of a NULL-terminated array of pointers to potential
  250.    matching strings.  The 1st element (matches[0]) is the maximal
  251.    substring that is common to all matches. This function can re-arrange
  252.    the list of matches as required, but all elements of the array must be
  253.    free()'d if they are deleted. The main intent of this function is
  254.    to implement FIGNORE a la SunOS csh. */
  255. Function *rl_ignore_some_completions_function = (Function *)NULL;
  256.  
  257. #if defined (SHELL)
  258. /* A function to strip quotes that are not protected by backquotes.  It
  259.    allows single quotes to appear within double quotes, and vice versa.
  260.    It should be smarter.  It's fairly shell-specific, hence the SHELL
  261.    definition wrapper. */
  262. static char *
  263. _delete_quotes (text)
  264.      char *text;
  265. {
  266.   char *ret, *p, *r;
  267.   int l, quoted;
  268.  
  269.   l = strlen (text);
  270.   ret = xmalloc (l + 1);
  271.   for (quoted = 0, p = text, r = ret; p && *p; p++)
  272.     {
  273.       /* Allow backslash-quoted characters to pass through unscathed. */
  274.       if (*p == '\\')
  275.         continue;
  276.       /* Close quote. */
  277.       if (quoted && *p == quoted)
  278.     {
  279.       quoted = 0;
  280.       continue;
  281.     }
  282.       /* Open quote. */
  283.       if (quoted == 0 && (*p == '\'' || *p == '"'))
  284.     {
  285.       quoted = *p;
  286.       continue;
  287.     }
  288.       *r++ = *p;
  289.     }
  290.   *r = '\0';
  291.   return ret;
  292. }
  293. #endif /* SHELL */
  294.  
  295. /* Return the portion of PATHNAME that should be output when listing
  296.    possible completions.  If we are hacking filename completion, we
  297.    are only interested in the basename, the portion following the
  298.    final slash.  Otherwise, we return what we were passed. */
  299. static char *
  300. printable_part (pathname)
  301.       char *pathname;
  302. {
  303.   char *temp = (char *)NULL;
  304.  
  305.   if (rl_filename_completion_desired)
  306.     temp = strrchr (pathname, '/');
  307.  
  308.   if (!temp)
  309.     return (pathname);
  310.   else
  311.     return (++temp);
  312. }
  313.  
  314. /* Output TO_PRINT to rl_outstream.  If VISIBLE_STATS is defined and we
  315.    are using it, check for and output a single character for `special'
  316.    filenames.  Return 1 if we printed an extension character, 0 if not. */
  317. #define PUTX(c) \
  318.       if (CTRL_CHAR (c)) \
  319.         { \
  320.           putc ('^', rl_outstream); \
  321.           putc (UNCTRL (c), rl_outstream); \
  322.         } \
  323.       else if (c == RUBOUT) \
  324.         { \
  325.           putc ('^', rl_outstream); \
  326.           putc ('?', rl_outstream); \
  327.         } \
  328.       else \
  329.         putc (c, rl_outstream)
  330.  
  331. static int
  332. print_filename (to_print, full_pathname)
  333.      char *to_print, *full_pathname;
  334. {
  335. #if !defined (VISIBLE_STATS)
  336.   char *s;
  337.  
  338.   for (s = to_print; *s; s++)
  339.     {
  340.       PUTX (*s);
  341.     }
  342.   return 0;
  343. #else  
  344.   char *s, c, *new_full_pathname;
  345.   int extension_char = 0, slen, tlen;
  346.  
  347.   for (s = to_print; *s; s++)
  348.     {
  349.       PUTX (*s);
  350.     }  
  351.  
  352.   if (rl_filename_completion_desired && rl_visible_stats)
  353.     {
  354.       /* If to_print != full_pathname, to_print is the basename of the
  355.      path passed.  In this case, we try to expand the directory
  356.      name before checking for the stat character. */
  357.       if (to_print != full_pathname)
  358.     {
  359.       /* Terminate the directory name. */
  360.       c = to_print[-1];
  361.       to_print[-1] = '\0';
  362.  
  363.       s = tilde_expand (full_pathname);
  364.       if (rl_directory_completion_hook)
  365.         (*rl_directory_completion_hook) (&s);
  366.  
  367.       slen = strlen (s);
  368.       tlen = strlen (to_print);
  369.       new_full_pathname = xmalloc (slen + tlen + 2);
  370.       strcpy (new_full_pathname, s);
  371.       new_full_pathname[slen] = '/';
  372.       strcpy (new_full_pathname + slen + 1, to_print);
  373.  
  374.       extension_char = stat_char (new_full_pathname);
  375.  
  376.       free (new_full_pathname);
  377.       to_print[-1] = c;
  378.     }
  379.       else
  380.     {
  381.       s = tilde_expand (full_pathname);
  382.       extension_char = stat_char (s);
  383.     }
  384.  
  385.       free (s);
  386.       if (extension_char)
  387.     putc (extension_char, rl_outstream);
  388.       return (extension_char != 0);
  389.     }
  390.   else
  391.     return 0;
  392. #endif /* VISIBLE_STATS */
  393. }
  394.  
  395. /* Complete the word at or before point.
  396.    WHAT_TO_DO says what to do with the completion.
  397.    `?' means list the possible completions.
  398.    TAB means do standard completion.
  399.    `*' means insert all of the possible completions.
  400.    `!' means to do standard completion, and list all possible completions if
  401.    there is more than one. */
  402. rl_complete_internal (what_to_do)
  403.      int what_to_do;
  404. {
  405.   char **matches;
  406.   Function *our_func;
  407.   int start, scan, end, delimiter = 0, pass_next;
  408.   char *text, *saved_line_buffer;
  409.   char *replacement;
  410.   char quote_char = '\0';
  411.   int found_quote = 0;
  412.  
  413.   if (rl_line_buffer)
  414.     saved_line_buffer = savestring (rl_line_buffer);
  415.   else
  416.     saved_line_buffer = (char *)NULL;
  417.  
  418.   if (rl_completion_entry_function)
  419.     our_func = rl_completion_entry_function;
  420.   else
  421.     our_func = (Function *)filename_completion_function;
  422.  
  423.   /* Only the completion entry function can change these. */
  424.   rl_filename_completion_desired = 0;
  425.   rl_filename_quoting_desired = 1;
  426.  
  427.   /* We now look backwards for the start of a filename/variable word. */
  428.   end = rl_point;
  429.  
  430.   if (rl_point)
  431.     {
  432.       if (rl_completer_quote_characters)
  433.     {
  434.       /* We have a list of characters which can be used in pairs to
  435.          quote substrings for the completer.  Try to find the start
  436.          of an unclosed quoted substring. */
  437.       /* FOUND_QUOTE is set so we know what kind of quotes we found. */
  438.       for (scan = pass_next = 0; scan < end; scan++)
  439.         {
  440.           if (pass_next)
  441.         {
  442.           pass_next = 0;
  443.           continue;
  444.         }
  445.  
  446.           if (rl_line_buffer[scan] == '\\')
  447.         {
  448.           pass_next = 1;
  449.           found_quote |= 4;
  450.           continue;
  451.         }
  452.  
  453.           if (quote_char != '\0')
  454.         {
  455.           /* Ignore everything until the matching close quote char. */
  456.           if (rl_line_buffer[scan] == quote_char)
  457.             {
  458.               /* Found matching close.  Abandon this substring. */
  459.               quote_char = '\0';
  460.               rl_point = end;
  461.             }
  462.         }
  463.           else if (strchr (rl_completer_quote_characters, rl_line_buffer[scan]))
  464.         {
  465.           /* Found start of a quoted substring. */
  466.           quote_char = rl_line_buffer[scan];
  467.           rl_point = scan + 1;
  468.           /* Shell-like quoting conventions. */
  469.           if (quote_char == '\'')
  470.             found_quote |= 1;
  471.           else if (quote_char == '"')
  472.             found_quote |= 2;
  473.         }
  474.         }
  475.     }
  476.  
  477.       if (rl_point == end && found_quote == 0)
  478.     {
  479.       int quoted = 0;
  480.       /* We didn't find an unclosed quoted substring upon which to do
  481.          completion, so use the word break characters to find the
  482.          substring on which to complete. */
  483.       while (--rl_point)
  484.         {
  485.           scan = rl_line_buffer[rl_point];
  486.  
  487.           if (strchr (rl_completer_word_break_characters, scan) == 0)
  488.         continue;
  489.  
  490. #if defined (SHELL)
  491.           /* Don't let word break characters in quoted substrings break
  492.          words for the completer. */
  493.           if (found_quote && char_is_quoted (rl_line_buffer, rl_point))
  494.         continue;
  495. #endif /* SHELL */
  496.  
  497.           /* Convoluted code, but it avoids an n^2 algorithm with calls
  498.                to char_is_quoted. */
  499.           break;
  500.         }
  501.     }
  502.  
  503.       /* If we are at an unquoted word break, then advance past it. */
  504.       scan = rl_line_buffer[rl_point];
  505. #if defined (SHELL)
  506.       if ((found_quote == 0 || char_is_quoted (rl_line_buffer, rl_point) == 0) &&
  507.           strchr (rl_completer_word_break_characters, scan))
  508. #else
  509.       if (strchr (rl_completer_word_break_characters, scan))
  510. #endif
  511.     {
  512.       /* If the character that caused the word break was a quoting
  513.          character, then remember it as the delimiter. */
  514.       if (strchr ("\"'", scan) && (end - rl_point) > 1)
  515.         delimiter = scan;
  516.  
  517.       /* If the character isn't needed to determine something special
  518.          about what kind of completion to perform, then advance past it. */
  519.       if (!rl_special_prefixes || strchr (rl_special_prefixes, scan) == 0)
  520.         rl_point++;
  521.     }
  522.     }
  523.  
  524.   /* At this point, we know we have an open quote if quote_char != '\0'. */
  525.   start = rl_point;
  526.   rl_point = end;
  527.   text = rl_copy_text (start, end);
  528.  
  529.   /* If the user wants to TRY to complete, but then wants to give
  530.      up and use the default completion function, they set the
  531.      variable rl_attempted_completion_function. */
  532.   if (rl_attempted_completion_function)
  533.     {
  534.       matches = (*rl_attempted_completion_function) (text, start, end);
  535.  
  536.       if (matches || rl_attempted_completion_over)
  537.     {
  538.       rl_attempted_completion_over = 0;
  539.       our_func = (Function *)NULL;
  540.       goto after_usual_completion;
  541.     }
  542.     }
  543.  
  544. #if defined (SHELL)
  545.   /* Beware -- we're stripping the quotes here.  Do this only if we know
  546.      we are doing filename completion. */
  547.   if (found_quote && our_func == (Function *)filename_completion_function)
  548.     {
  549.       /* delete single and double quotes */
  550.       replacement = _delete_quotes (text);
  551.       free (text);
  552.       text = replacement;
  553.       replacement = (char *)0;
  554.     }
  555. #endif /* SHELL */
  556.  
  557.   matches = completion_matches (text, our_func);
  558.  
  559.  after_usual_completion:
  560.   free (text);
  561.  
  562.   if (!matches)
  563.     ding ();
  564.   else
  565.     {
  566.       register int i;
  567.       int should_quote;
  568.  
  569.       /* It seems to me that in all the cases we handle we would like
  570.      to ignore duplicate possiblilities.  Scan for the text to
  571.      insert being identical to the other completions. */
  572.       if (rl_ignore_completion_duplicates)
  573.     {
  574.       char *lowest_common;
  575.       int j, newlen = 0;
  576.       char dead_slot;
  577.       char **temp_array;
  578.  
  579.       /* Sort the items. */
  580.       /* It is safe to sort this array, because the lowest common
  581.          denominator found in matches[0] will remain in place. */
  582.       for (i = 0; matches[i]; i++)
  583.         ;
  584.       /* Try sorting the array without matches[0], since we need it to
  585.          stay in place no matter what. */
  586.       if (i)
  587.         qsort (matches+1, i-1, sizeof (char *), compare_strings);
  588.  
  589.       /* Remember the lowest common denominator for it may be unique. */
  590.       lowest_common = savestring (matches[0]);
  591.  
  592.       for (i = 0; matches[i + 1]; i++)
  593.         {
  594.           if (strcmp (matches[i], matches[i + 1]) == 0)
  595.         {
  596.           free (matches[i]);
  597.           matches[i] = (char *)&dead_slot;
  598.         }
  599.           else
  600.         newlen++;
  601.         }
  602.  
  603.       /* We have marked all the dead slots with (char *)&dead_slot.
  604.          Copy all the non-dead entries into a new array. */
  605.       temp_array = (char **)xmalloc ((3 + newlen) * sizeof (char *));
  606.       for (i = j = 1; matches[i]; i++)
  607.         {
  608.           if (matches[i] != (char *)&dead_slot)
  609.         temp_array[j++] = matches[i];
  610.         }
  611.       temp_array[j] = (char *)NULL;
  612.  
  613.       if (matches[0] != (char *)&dead_slot)
  614.         free (matches[0]);
  615.       free (matches);
  616.  
  617.       matches = temp_array;
  618.  
  619.       /* Place the lowest common denominator back in [0]. */
  620.       matches[0] = lowest_common;
  621.  
  622.       /* If there is one string left, and it is identical to the
  623.          lowest common denominator, then the LCD is the string to
  624.          insert. */
  625.       if (j == 2 && strcmp (matches[0], matches[1]) == 0)
  626.         {
  627.           free (matches[1]);
  628.           matches[1] = (char *)NULL;
  629.         }
  630.     }
  631.  
  632.       switch (what_to_do)
  633.     {
  634.     case TAB:
  635.     case '!':
  636.       /* If we are matching filenames, then here is our chance to
  637.          do clever processing by re-examining the list.  Call the
  638.          ignore function with the array as a parameter.  It can
  639.          munge the array, deleting matches as it desires. */
  640.       if (rl_ignore_some_completions_function &&
  641.           our_func == (Function *)filename_completion_function)
  642.         (void)(*rl_ignore_some_completions_function)(matches);
  643.  
  644.       /* If we are doing completion on quoted substrings, and any matches
  645.          contain any of the completer_word_break_characters, then auto-
  646.          matically prepend the substring with a quote character (just pick
  647.          the first one from the list of such) if it does not already begin
  648.          with a quote string.  FIXME: Need to remove any such automatically
  649.          inserted quote character when it no longer is necessary, such as
  650.          if we change the string we are completing on and the new set of
  651.          matches don't require a quoted substring. */
  652.       replacement = matches[0];
  653.  
  654.       should_quote = matches[0] && rl_completer_quote_characters &&
  655.              rl_filename_completion_desired &&
  656.              rl_filename_quoting_desired;
  657.  
  658.       if (should_quote)
  659. #if defined (SHELL)
  660.         should_quote = should_quote && (!quote_char || quote_char == '"');
  661. #else
  662.         should_quote = should_quote && !quote_char;
  663. #endif
  664.  
  665.       if (should_quote)
  666.         {
  667.           int do_replace;
  668.  
  669.           do_replace = NO_MATCH;
  670.  
  671.           /* If there is a single match, see if we need to quote it.
  672.          This also checks whether the common prefix of several
  673.          matches needs to be quoted.  If the common prefix should
  674.          not be checked, add !matches[1] to the if clause. */
  675.           should_quote = rl_strpbrk (matches[0], rl_completer_word_break_characters) != 0;
  676. #if defined (SHELL)
  677.           should_quote = should_quote || rl_strpbrk (matches[0], "#$`?*[!") != 0;
  678. #endif
  679.  
  680.           if (should_quote)
  681.         do_replace = matches[1] ? MULT_MATCH : SINGLE_MATCH;
  682.  
  683.           if (do_replace != NO_MATCH)
  684.         {
  685. #if defined (SHELL)
  686.           /* Quote the replacement, since we found an
  687.              embedded word break character in a potential
  688.              match. */
  689.           char *rtext, *mtext;
  690.           int rlen;
  691.           extern char *double_quote ();    /* in builtins/common.c */
  692.  
  693.           /* If DO_REPLACE == MULT_MATCH, it means that there is
  694.              more than one match.  In this case, we do not add
  695.              the closing quote or attempt to perform tilde
  696.              expansion.  If DO_REPLACE == SINGLE_MATCH, we try
  697.              to perform tilde expansion, because double quotes
  698.              inhibit tilde expansion by the shell. */
  699.  
  700.           mtext = matches[0];
  701.           if (mtext[0] == '~' && do_replace == SINGLE_MATCH)
  702.             mtext = tilde_expand (matches[0]);
  703.           rtext = double_quote (mtext);
  704.           if (mtext != matches[0])
  705.             free (mtext);
  706.  
  707.           rlen = strlen (rtext);
  708.           replacement = xmalloc (rlen + 1);
  709.           /* If we're completing on a quoted string where the user
  710.              has already supplied the opening quote, we don't want
  711.              the quote in the replacement text, and we reset
  712.              QUOTE_CHAR to 0 to avoid an extra closing quote. */
  713.           if (quote_char == '"')
  714.             {
  715.               strcpy (replacement, rtext + 1);
  716.               rlen--;
  717.               quote_char = 0;
  718.             }
  719.           else
  720.             strcpy (replacement, rtext);
  721.           if (do_replace == MULT_MATCH)
  722.             replacement[rlen - 1] = '\0';
  723.           free (rtext);
  724. #else /* !SHELL */
  725.           /* Found an embedded word break character in a potential
  726.              match, so we need to prepend a quote character if we
  727.              are replacing the completion string. */
  728.           replacement = xmalloc (strlen (matches[0]) + 2);
  729.           quote_char = *rl_completer_quote_characters;
  730.           *replacement = quote_char;
  731.           strcpy (replacement + 1, matches[0]);
  732. #endif /* SHELL */
  733.         }
  734.         }
  735.  
  736.       if (replacement)
  737.         {
  738.           rl_begin_undo_group ();
  739.           rl_delete_text (start, rl_point);
  740.           rl_point = start;
  741.           rl_insert_text (replacement);
  742.           rl_end_undo_group ();
  743.           if (replacement != matches[0])
  744.         free (replacement);
  745.         }
  746.  
  747.       /* If there are more matches, ring the bell to indicate.
  748.          If this was the only match, and we are hacking files,
  749.          check the file to see if it was a directory.  If so,
  750.          add a '/' to the name.  If not, and we are at the end
  751.          of the line, then add rl_completion_append_character,
  752.          usually a space. */
  753.       if (matches[1])
  754.         {
  755.           if (what_to_do == '!')
  756.         goto display_matches;        /* XXX */
  757.           else if (rl_editing_mode != vi_mode)
  758.         ding ();    /* There are other matches remaining. */
  759.         }
  760.       else
  761.         {
  762.           char temp_string[4];
  763.           int temp_string_index = 0;
  764.  
  765.           if (quote_char)
  766.         temp_string[temp_string_index++] = quote_char;
  767.  
  768.           temp_string[temp_string_index++] = delimiter ? delimiter
  769.         : rl_completion_append_character;
  770.  
  771.           if (rl_completion_append_character)
  772.         temp_string[temp_string_index++] = '\0';
  773.  
  774.           if (rl_filename_completion_desired)
  775.         {
  776.           struct stat finfo;
  777.           char *filename = tilde_expand (matches[0]);
  778.  
  779.           if ((stat (filename, &finfo) == 0) && S_ISDIR (finfo.st_mode))
  780.             {
  781.               if (rl_line_buffer[rl_point] != '/')
  782.             rl_insert_text ("/");
  783.             }
  784.           else
  785.             {
  786.               if (rl_point == rl_end)
  787.             rl_insert_text (temp_string);
  788.             }
  789.           free (filename);
  790.         }
  791.           else
  792.         {
  793.           if (rl_point == rl_end)
  794.             rl_insert_text (temp_string);
  795.         }
  796.         }
  797.       break;
  798.  
  799.     case '*':
  800.       {
  801.         int i = 1;
  802.  
  803.         rl_begin_undo_group ();
  804.         rl_delete_text (start, rl_point);
  805.         rl_point = start;
  806.         if (matches[1])
  807.           {
  808.         while (matches[i])
  809.           {
  810.             rl_insert_text (matches[i++]);
  811.             rl_insert_text (" ");
  812.           }
  813.           }
  814.         else
  815.           {
  816.         rl_insert_text (matches[0]);
  817.         rl_insert_text (" ");
  818.           }
  819.         rl_end_undo_group ();
  820.       }
  821.       break;
  822.  
  823.     case '?':
  824.       {
  825.         int len, count, limit, max;
  826.         int j, k, l;
  827.  
  828.         /* Handle simple case first.  What if there is only one answer? */
  829.         if (!matches[1])
  830.           {
  831.         char *temp;
  832.  
  833.         temp = printable_part (matches[0]);
  834.         crlf ();
  835.         print_filename (temp, matches[0]);
  836.         crlf ();
  837.         goto restart;
  838.           }
  839.  
  840.         /* There is more than one answer.  Find out how many there are,
  841.            and find out what the maximum printed length of a single entry
  842.            is. */
  843.       display_matches:
  844.         for (max = 0, i = 1; matches[i]; i++)
  845.           {
  846.         char *temp;
  847.         int name_length;
  848.  
  849.         temp = printable_part (matches[i]);
  850.         name_length = strlen (temp);
  851.  
  852.         if (name_length > max)
  853.           max = name_length;
  854.           }
  855.  
  856.         len = i - 1;
  857.  
  858.         /* If there are many items, then ask the user if she
  859.            really wants to see them all. */
  860.         if (len >= rl_completion_query_items)
  861.           {
  862.         crlf ();
  863.         fprintf (rl_outstream,
  864.              "There are %d possibilities.  Do you really", len);
  865.         crlf ();
  866.         fprintf (rl_outstream, "wish to see them all? (y or n)");
  867.         fflush (rl_outstream);
  868.         if (!get_y_or_n ())
  869.           {
  870.             crlf ();
  871.             goto restart;
  872.           }
  873.           }
  874.  
  875.         /* How many items of MAX length can we fit in the screen window? */
  876.         max += 2;
  877.         limit = screenwidth / max;
  878.         if (limit != 1 && (limit * max == screenwidth))
  879.           limit--;
  880.  
  881.         /* Avoid a possible floating exception.  If max > screenwidth,
  882.            limit will be 0 and a divide-by-zero fault will result. */
  883.         if (limit == 0)
  884.           limit = 1;
  885.  
  886.         /* How many iterations of the printing loop? */
  887.         count = (len + (limit - 1)) / limit;
  888.  
  889.         /* Watch out for special case.  If LEN is less than LIMIT, then
  890.            just do the inner printing loop.
  891.            0 < len <= limit  implies  count = 1. */
  892.  
  893.         /* Sort the items if they are not already sorted. */
  894.         if (!rl_ignore_completion_duplicates)
  895.           qsort (matches + 1, len - 1, sizeof (char *), compare_strings);
  896.  
  897.         /* Print the sorted items, up-and-down alphabetically, like
  898.            ls might. */
  899.         crlf ();
  900.  
  901.         for (i = 1; i <= count; i++)
  902.           {
  903.         for (j = 0, l = i; j < limit; j++)
  904.           {
  905.             if (l > len || !matches[l])
  906.               break;
  907.             else
  908.               {
  909.             char *temp;
  910.             int printed_length;
  911.  
  912.             temp = printable_part (matches[l]);
  913.             printed_length = strlen (temp);
  914.             printed_length += print_filename (temp, matches[l]);
  915.  
  916.             if (j + 1 < limit)
  917.               {
  918.                 for (k = 0; k < max - printed_length; k++)
  919.                   putc (' ', rl_outstream);
  920.               }
  921.               }
  922.             l += count;
  923.           }
  924.         crlf ();
  925.           }
  926.       restart:
  927.  
  928.         rl_on_new_line ();
  929.       }
  930.       break;
  931.  
  932.     default:
  933.       fprintf (stderr, "\r\nreadline: bad value for what_to_do in rl_complete\n");
  934.       abort ();
  935.     }
  936.  
  937.       for (i = 0; matches[i]; i++)
  938.     free (matches[i]);
  939.       free (matches);
  940.     }
  941.  
  942.   /* Check to see if the line has changed through all of this manipulation. */
  943.   if (saved_line_buffer)
  944.     {
  945.       if (strcmp (rl_line_buffer, saved_line_buffer) != 0)
  946.     completion_changed_buffer = 1;
  947.       else
  948.     completion_changed_buffer = 0;
  949.  
  950.       free (saved_line_buffer);
  951.     }
  952.   return 0;
  953. }
  954.  
  955. #if defined (VISIBLE_STATS)
  956. /* Return the character which best describes FILENAME.
  957.      `@' for symbolic links
  958.      `/' for directories
  959.      `*' for executables
  960.      `=' for sockets */
  961. static int
  962. stat_char (filename)
  963.      char *filename;
  964. {
  965.   struct stat finfo;
  966.   int character, r;
  967.  
  968. #if defined (S_ISLNK)
  969.   r = lstat (filename, &finfo);
  970. #else
  971.   r = stat (filename, &finfo);
  972. #endif
  973.  
  974.   if (r == -1)
  975.     return (0);
  976.  
  977.   character = 0;
  978.   if (S_ISDIR (finfo.st_mode))
  979.     character = '/';
  980. #if defined (S_ISLNK)
  981.   else if (S_ISLNK (finfo.st_mode))
  982.     character = '@';
  983. #endif /* S_ISLNK */
  984. #if defined (S_ISSOCK)
  985.   else if (S_ISSOCK (finfo.st_mode))
  986.     character = '=';
  987. #endif /* S_ISSOCK */
  988.   else if (S_ISREG (finfo.st_mode))
  989.     {
  990.       if (access (filename, X_OK) == 0)
  991.     character = '*';
  992.     }
  993.   return (character);
  994. }
  995. #endif /* VISIBLE_STATS */
  996.  
  997. /* Stupid comparison routine for qsort () ing strings. */
  998. static int
  999. compare_strings (s1, s2)
  1000.   char **s1, **s2;
  1001. {
  1002.   int result;
  1003.  
  1004.   result = **s1 - **s2;
  1005.   if (result == 0)
  1006.     result = strcmp (*s1, *s2);
  1007.  
  1008.   return result;
  1009. }
  1010.  
  1011. /* A completion function for usernames.
  1012.    TEXT contains a partial username preceded by a random
  1013.    character (usually `~').  */
  1014. char *
  1015. username_completion_function (text, state)
  1016.      int state;
  1017.      char *text;
  1018. {
  1019. #if defined (__GO32__)
  1020.   return (char *)NULL;
  1021. #else /* !__GO32__ */
  1022.   static char *username = (char *)NULL;
  1023.   static struct passwd *entry;
  1024.   static int namelen, first_char, first_char_loc;
  1025.  
  1026.   if (!state)
  1027.     {
  1028.       if (username)
  1029.     free (username);
  1030.  
  1031.       first_char = *text;
  1032.  
  1033.       if (first_char == '~')
  1034.     first_char_loc = 1;
  1035.       else
  1036.     first_char_loc = 0;
  1037.  
  1038.       username = savestring (&text[first_char_loc]);
  1039.       namelen = strlen (username);
  1040.       setpwent ();
  1041.     }
  1042.  
  1043.   while (entry = getpwent ())
  1044.     {
  1045.       /* Null usernames should result in all users as possible completions. */
  1046.       if (namelen == 0)
  1047.         break;
  1048.       else if ((username[0] == entry->pw_name[0]) &&
  1049.            (strncmp (username, entry->pw_name, namelen) == 0))
  1050.     break;
  1051.     }
  1052.  
  1053.   if (!entry)
  1054.     {
  1055.       endpwent ();
  1056.       return ((char *)NULL);
  1057.     }
  1058.   else
  1059.     {
  1060.       char *value = xmalloc (2 + strlen (entry->pw_name));
  1061.  
  1062.       *value = *text;
  1063.  
  1064.       strcpy (value + first_char_loc, entry->pw_name);
  1065.  
  1066.       if (first_char == '~')
  1067.     rl_filename_completion_desired = 1;
  1068.  
  1069.       return (value);
  1070.     }
  1071. #endif /* !__GO32__ */
  1072. }
  1073.  
  1074. /* **************************************************************** */
  1075. /*                                    */
  1076. /*                 Completion                    */
  1077. /*                                    */
  1078. /* **************************************************************** */
  1079.  
  1080. /* Non-zero means that case is not significant in completion. */
  1081. int completion_case_fold = 0;
  1082.  
  1083. /* Return an array of (char *) which is a list of completions for TEXT.
  1084.    If there are no completions, return a NULL pointer.
  1085.    The first entry in the returned array is the substitution for TEXT.
  1086.    The remaining entries are the possible completions.
  1087.    The array is terminated with a NULL pointer.
  1088.  
  1089.    ENTRY_FUNCTION is a function of two args, and returns a (char *).
  1090.      The first argument is TEXT.
  1091.      The second is a state argument; it should be zero on the first call, and
  1092.      non-zero on subsequent calls.  It returns a NULL pointer to the caller
  1093.      when there are no more matches.
  1094.  */
  1095. char **
  1096. completion_matches (text, entry_function)
  1097.      char *text;
  1098.      CPFunction *entry_function;
  1099. {
  1100.   /* Number of slots in match_list. */
  1101.   int match_list_size;
  1102.  
  1103.   /* The list of matches. */
  1104.   char **match_list =
  1105.     (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
  1106.  
  1107.   /* Number of matches actually found. */
  1108.   int matches = 0;
  1109.  
  1110.   /* Temporary string binder. */
  1111.   char *string;
  1112.  
  1113.   match_list[1] = (char *)NULL;
  1114.  
  1115.   while (string = (*entry_function) (text, matches))
  1116.     {
  1117.       if (matches + 1 == match_list_size)
  1118.     match_list = (char **)xrealloc
  1119.       (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
  1120.  
  1121.       match_list[++matches] = string;
  1122.       match_list[matches + 1] = (char *)NULL;
  1123.     }
  1124.  
  1125.   /* If there were any matches, then look through them finding out the
  1126.      lowest common denominator.  That then becomes match_list[0]. */
  1127.   if (matches)
  1128.     {
  1129.       register int i = 1;
  1130.       int low = 100000;        /* Count of max-matched characters. */
  1131.  
  1132.       /* If only one match, just use that. */
  1133.       if (matches == 1)
  1134.     {
  1135.       match_list[0] = match_list[1];
  1136.       match_list[1] = (char *)NULL;
  1137.     }
  1138.       else
  1139.     {
  1140.       /* Otherwise, compare each member of the list with
  1141.          the next, finding out where they stop matching. */
  1142.  
  1143.       while (i < matches)
  1144.         {
  1145.           register int c1, c2, si;
  1146.  
  1147.           if (completion_case_fold)
  1148.         {
  1149.           for (si = 0;
  1150.                (c1 = to_lower(match_list[i][si])) &&
  1151.                (c2 = to_lower(match_list[i + 1][si]));
  1152.                si++)
  1153.             if (c1 != c2) break;
  1154.         }
  1155.           else
  1156.         {
  1157.           for (si = 0;
  1158.                (c1 = match_list[i][si]) &&
  1159.                (c2 = match_list[i + 1][si]);
  1160.                si++)
  1161.             if (c1 != c2) break;
  1162.         }
  1163.  
  1164.           if (low > si) low = si;
  1165.           i++;
  1166.         }
  1167.       match_list[0] = xmalloc (low + 1);
  1168.       strncpy (match_list[0], match_list[1], low);
  1169.       match_list[0][low] = '\0';
  1170.     }
  1171.     }
  1172.   else                /* There were no matches. */
  1173.     {
  1174.       free (match_list);
  1175.       match_list = (char **)NULL;
  1176.     }
  1177.   return (match_list);
  1178. }
  1179.  
  1180. /* Okay, now we write the entry_function for filename completion.  In the
  1181.    general case.  Note that completion in the shell is a little different
  1182.    because of all the pathnames that must be followed when looking up the
  1183.    completion for a command. */
  1184. char *
  1185. filename_completion_function (text, state)
  1186.      int state;
  1187.      char *text;
  1188. {
  1189.   static DIR *directory;
  1190.   static char *filename = (char *)NULL;
  1191.   static char *dirname = (char *)NULL;
  1192.   static char *users_dirname = (char *)NULL;
  1193.   static int filename_len;
  1194.  
  1195.   struct dirent *entry = (struct dirent *)NULL;
  1196.  
  1197.   /* If we don't have any state, then do some initialization. */
  1198.   if (!state)
  1199.     {
  1200.       char *temp;
  1201.  
  1202.       if (dirname) free (dirname);
  1203.       if (filename) free (filename);
  1204.       if (users_dirname) free (users_dirname);
  1205.  
  1206.       filename = savestring (text);
  1207.       if (!*text) text = ".";
  1208.       dirname = savestring (text);
  1209.  
  1210.       temp = strrchr (dirname, '/');
  1211.  
  1212.       if (temp)
  1213.     {
  1214.       strcpy (filename, ++temp);
  1215.       *temp = '\0';
  1216.     }
  1217.       else
  1218.     strcpy (dirname, ".");
  1219.  
  1220.       /* We aren't done yet.  We also support the "~user" syntax. */
  1221.  
  1222.       /* Save the version of the directory that the user typed. */
  1223.       users_dirname = savestring (dirname);
  1224.       {
  1225.     char *temp_dirname;
  1226.     int replace_dirname;
  1227.  
  1228.     temp_dirname = tilde_expand (dirname);
  1229.     free (dirname);
  1230.     dirname = temp_dirname;
  1231.  
  1232.     replace_dirname = 0;
  1233.     if (rl_directory_completion_hook)
  1234.       replace_dirname = (*rl_directory_completion_hook) (&dirname);
  1235.  
  1236.     if (replace_dirname)
  1237.       {
  1238.         free (users_dirname);
  1239.         users_dirname = savestring (dirname);
  1240.       }
  1241.       }
  1242.       directory = opendir (dirname);
  1243.       filename_len = strlen (filename);
  1244.  
  1245.       rl_filename_completion_desired = 1;
  1246.     }
  1247.  
  1248.   /* At this point we should entertain the possibility of hacking wildcarded
  1249.      filenames, like /usr/man/man<WILD>/te<TAB>.  If the directory name
  1250.      contains globbing characters, then build an array of directories, and
  1251.      then map over that list while completing. */
  1252.   /* *** UNIMPLEMENTED *** */
  1253.  
  1254.   /* Now that we have some state, we can read the directory. */
  1255.  
  1256.   while (directory && (entry = readdir (directory)))
  1257.     {
  1258.       /* Special case for no filename.
  1259.      All entries except "." and ".." match. */
  1260.       if (!filename_len)
  1261.     {
  1262.       if ((strcmp (entry->d_name, ".") != 0) &&
  1263.           (strcmp (entry->d_name, "..") != 0))
  1264.         break;
  1265.     }
  1266.       else
  1267.     {
  1268.       /* Otherwise, if these match up to the length of filename, then
  1269.          it is a match. */
  1270.         if ((entry->d_name[0] == filename[0]) &&
  1271.         (((int)D_NAMLEN (entry)) >= filename_len) &&
  1272.         (strncmp (filename, entry->d_name, filename_len) == 0))
  1273.           break;
  1274.     }
  1275.     }
  1276.  
  1277.   if (!entry)
  1278.     {
  1279.       if (directory)
  1280.     {
  1281.       closedir (directory);
  1282.       directory = (DIR *)NULL;
  1283.     }
  1284.       if (dirname)
  1285.     {
  1286.       free (dirname);
  1287.       dirname = (char *)NULL;
  1288.     }
  1289.       if (filename)
  1290.     {
  1291.       free (filename);
  1292.       filename = (char *)NULL;
  1293.     }
  1294.       if (users_dirname)
  1295.     {
  1296.       free (users_dirname);
  1297.       users_dirname = (char *)NULL;
  1298.     }
  1299.  
  1300.       return (char *)NULL;
  1301.     }
  1302.   else
  1303.     {
  1304.       char *temp;
  1305.  
  1306.       /* dirname && (strcmp (dirname, ".") != 0) */
  1307.       if (dirname && (dirname[0] != '.' || dirname[1]))
  1308.     {
  1309.       if (rl_complete_with_tilde_expansion && *users_dirname == '~')
  1310.         {
  1311.           int dirlen = strlen (dirname);
  1312.           temp = xmalloc (2 + dirlen + D_NAMLEN (entry));
  1313.           strcpy (temp, dirname);
  1314.           /* Canonicalization cuts off any final slash present.  We need
  1315.          to add it back. */
  1316.           if (dirname[dirlen - 1] != '/')
  1317.             {
  1318.               temp[dirlen] = '/';
  1319.               temp[dirlen + 1] = '\0';
  1320.             }
  1321.         }
  1322.       else
  1323.         {
  1324.           temp = xmalloc (1 + strlen (users_dirname) + D_NAMLEN (entry));
  1325.           strcpy (temp, users_dirname);
  1326.         }
  1327.  
  1328.       strcat (temp, entry->d_name);
  1329.     }
  1330.       else
  1331.     temp = (savestring (entry->d_name));
  1332.  
  1333.       return (temp);
  1334.     }
  1335. }
  1336.  
  1337. /* A function for simple tilde expansion. */
  1338. int
  1339. rl_tilde_expand (ignore, key)
  1340.      int ignore, key;
  1341. {
  1342.   register int start, end;
  1343.   char *homedir;
  1344.  
  1345.   end = rl_point;
  1346.   start = end - 1;
  1347.  
  1348.   if (rl_point == rl_end && rl_line_buffer[rl_point] == '~')
  1349.     {
  1350.       homedir = tilde_expand ("~");
  1351.       goto insert;
  1352.     }
  1353.   else if (rl_line_buffer[start] != '~')
  1354.     {
  1355.       for (; !whitespace (rl_line_buffer[start]) && start >= 0; start--);
  1356.       start++;
  1357.     }
  1358.  
  1359.   end = start;
  1360.   do
  1361.     {
  1362.       end++;
  1363.     }
  1364.   while (!whitespace (rl_line_buffer[end]) && end < rl_end);
  1365.  
  1366.   if (whitespace (rl_line_buffer[end]) || end >= rl_end)
  1367.     end--;
  1368.  
  1369.   /* If the first character of the current word is a tilde, perform
  1370.      tilde expansion and insert the result.  If not a tilde, do
  1371.      nothing. */
  1372.   if (rl_line_buffer[start] == '~')
  1373.     {
  1374.       char *temp;
  1375.       int len;
  1376.  
  1377.       len = end - start + 1;
  1378.       temp = xmalloc (len + 1);
  1379.       strncpy (temp, rl_line_buffer + start, len);
  1380.       temp[len] = '\0';
  1381.       homedir = tilde_expand (temp);
  1382.       free (temp);
  1383.  
  1384.     insert:
  1385.       rl_begin_undo_group ();
  1386.       rl_delete_text (start, end + 1);
  1387.       rl_point = start;
  1388.       rl_insert_text (homedir);
  1389.       rl_end_undo_group ();
  1390.     }
  1391.  
  1392.   return (0);
  1393. }
  1394.  
  1395. /* Find the first occurrence in STRING1 of any character from STRING2.
  1396.    Return a pointer to the character in STRING1. */
  1397. static char *
  1398. rl_strpbrk (string1, string2)
  1399.      char *string1, *string2;
  1400. {
  1401.   register char *scan;
  1402.  
  1403.   for (; *string1; string1++)
  1404.     {
  1405.       for (scan = string2; *scan; scan++)
  1406.     {
  1407.       if (*string1 == *scan)
  1408.         {
  1409.           return (string1);
  1410.         }
  1411.     }
  1412.     }
  1413.   return ((char *)NULL);
  1414. }
  1415.  
  1416. #if defined (STATIC_MALLOC)
  1417.  
  1418. /* **************************************************************** */
  1419. /*                                    */
  1420. /*            xmalloc and xrealloc ()                     */
  1421. /*                                    */
  1422. /* **************************************************************** */
  1423.  
  1424. static void memory_error_and_abort ();
  1425.  
  1426. static char *
  1427. xmalloc (bytes)
  1428.      int bytes;
  1429. {
  1430.   char *temp = (char *)malloc (bytes);
  1431.  
  1432.   if (!temp)
  1433.     memory_error_and_abort ();
  1434.   return (temp);
  1435. }
  1436.  
  1437. static char *
  1438. xrealloc (pointer, bytes)
  1439.      char *pointer;
  1440.      int bytes;
  1441. {
  1442.   char *temp;
  1443.  
  1444.   if (!pointer)
  1445.     temp = (char *)malloc (bytes);
  1446.   else
  1447.     temp = (char *)realloc (pointer, bytes);
  1448.  
  1449.   if (!temp)
  1450.     memory_error_and_abort ();
  1451.  
  1452.   return (temp);
  1453. }
  1454.  
  1455. static void
  1456. memory_error_and_abort ()
  1457. {
  1458.   fprintf (stderr, "readline: Out of virtual memory!\n");
  1459.   abort ();
  1460. }
  1461. #endif /* STATIC_MALLOC */
  1462.